﻿using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using Microsoft.AspNetCore.Authentication;
using T.E.ERP.Read.API.Applicantions.Command.ProcurementsCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 查询开票通知单的所有信息处理方法
    /// </summary>
    public class GetInvoiceSettlementEntityCommandHandler:IRequestHandler<GetInvoiceSettlementEntityCommand,APIResult<List<InvoiceDtos>>>
    {
        /// <summary>
        /// 开票通知单的仓储接口
        /// </summary>
        private readonly IBaseRepository<InvoiceSettlementEntity> iRepo;
        /// <summary>
        /// 订单物料表的仓储接口
        /// </summary>
        private readonly IBaseRepository<OrderMaterials> oRepo;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="iRepo"></param>
        public GetInvoiceSettlementEntityCommandHandler(IBaseRepository<InvoiceSettlementEntity>iRepo,IBaseRepository<OrderMaterials>oRepo)
        {
            this.iRepo = iRepo;
            this.oRepo = oRepo;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<InvoiceDtos>>> Handle(GetInvoiceSettlementEntityCommand request, CancellationToken cancellationToken)
        {                   
            var list = from a in iRepo.GetAll().Result
                       join b in oRepo.GetAll().Result on a.DocumentCode equals b.SaleCode
                       select new InvoiceDtos
                       {
                           InvoiceSettlementEntityId = a.InvoiceSettlementEntityId,
                           DocumentCode = a.DocumentCode,
                           DocumentDate = a.DocumentDate,
                           DocumentStatus = a.DocumentStatus,
                           BusinessType = a.BusinessType,
                           CustomerName = a.CustomerName,
                           MaterialsCode = a.MaterialsCode,
                           MaterialsName = b.MaterialsName,
                           CalulateNumber = b.CalulateNumber,
                           Price = b.Price,
                           TotalPrice = b.TotalPrice,
                           Bank = a.Bank,
                           Operator = a.Operator,
                           ShenPiStatus =a.ShenPiStatus,
                       };
            if(request.MaterialsCode!=null)
            {
                list = list.Where(x=>x.MaterialsCode==request.MaterialsCode);
            }
            if(request.DocumentCode!=null)
            {
                list = list.Where(x => x.DocumentCode == request.DocumentCode);
            }
			list = list.OrderByDescending(x => x.InvoiceSettlementEntityId).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize);
			APIResult<List<InvoiceDtos>> result = new APIResult<List<InvoiceDtos>>();
			result.Code = APIEnum.成功;
			result.Msg = APIEnum.查询成功.ToString();
			result.data = list.ToList();
			result.Count = list.Count();

			return await Task.FromResult(result);






        }



    }



}
